package com.taikang.system.controller.admin;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.json.simple.JSONObject;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.taikang.auth.entity.User;
import com.taikang.auth.service.IUserService;
import com.taikang.base.controller.BaseController;
import com.taikang.base.token.TokenValidate;
import com.taikang.base.util.NumberUtil;
import com.taikang.system.entity.SysRegionCountry;
import com.taikang.system.form.SysRegionCountryForm;
import com.taikang.system.service.ISysRegionCountryService;


@Controller("sysRegionCountryController")
@Scope("prototype")
@RequestMapping("/admin/system/region/country")
public class SysRegionCountryController extends BaseController {

	@Resource(name = "sysRegionCountryServiceImpl")
	private ISysRegionCountryService sysRegionCountryService;
	@Resource(name = "userServiceImpl")
	private IUserService userService;

	/**
	 * 首页
	 * @return 
	 */
	@RequestMapping(value = "/index.htm", method = RequestMethod.GET)
	public String index(){
		return "/admin/system/sys_region_country_index";
	}

	/**
	 * 分页查询
	 * @return 
	 */
	@RequestMapping(value = "/listSysRegionCountryByPage.htm", method = RequestMethod.POST)
	public void listSysRegionCountryByPage(@ModelAttribute SysRegionCountryForm model,HttpServletRequest request,HttpServletResponse response,HttpSession session){
		try{
			sysRegionCountryService.listSysRegionCountryByPage(model.getPageResult(),model.getCondition());
			JSONObject root = toPageJson(model.getPageResult(), new String[]{"id","name","sort"});
			ajaxPageResult(root,response);
		}catch(Exception ex){
			String msg = "查询国家管理时发生异常："+ex.getMessage();
			logger.error(msg,ex);
		}	}

	/**
	 * 保存国家管理
	 * @return 
	 */
	@RequestMapping(value = "/edit.htm", method = RequestMethod.GET)
	public ModelAndView edit(@ModelAttribute SysRegionCountryForm model,HttpServletRequest request,HttpServletResponse response,HttpSession session){
		ModelAndView view = new ModelAndView();
		view.setViewName("/admin/system/sys_region_country_add");
		Map<String,Object> data = view.getModel();
		if(null != model.getItem().getId()){
			SysRegionCountry item = sysRegionCountryService.getSysRegionCountryById(model.getItem().getId());
			data.put("item", item);
		}

		return view;
	}

	/**
	 * 保存国家管理
	 * @return 
	 */
	@RequestMapping(value = "/saveSysRegionCountry.htm", method = RequestMethod.POST)
	@TokenValidate
	public ModelAndView saveSysRegionCountry(@ModelAttribute SysRegionCountryForm model,HttpServletRequest request,HttpSession session){
		try {
			User user = userService.getCurrentUser();
			
			if(null == model.getItem().getId()){
				model.getItem().setCreateBy(user.getAccount());
				model.getItem().setUpdateBy(user.getAccount());
				sysRegionCountryService.add(model.getItem());
			}else{
				model.getItem().setUpdateBy(user.getAccount());
				sysRegionCountryService.update(model.getItem());
			}
			return ajaxJSON(Status.success,"保存成功");
		} catch (Exception e) {
			String msg = "保存国家管理时发生异常：" + e.getMessage();
			logger.error(msg,e);
			return ajaxJSON(Status.error, "保存失败，请稍后重试");
		}
	}
	/**
	 * 删除SysRegionCountry
	 * @return 
	 */
	@RequestMapping(value = "/del.htm", method = RequestMethod.POST)
	public ModelAndView del(@ModelAttribute SysRegionCountryForm model,HttpServletRequest request,HttpServletResponse response,HttpSession session){
		try {
			sysRegionCountryService.delByIds(NumberUtil.convertStringArrayToLongArray(model.getSelIds().split(",")));
			return ajaxJSON(Status.success,"删除成功");
		} catch (Exception e) {
			String msg = "删除国家管理时发生异常：" + e.getMessage();
			logger.error(msg,e);
			return ajaxJSON(Status.error, "删除失败，请稍后重试");
		}
	}

}

